{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 量子计算实验\n",
    "## 一、制造并观测单量子的叠加态"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "本地仿真的原理是首先通过复数运算得到观测前的量子态，再通过计算机内部基于经典电路的算法产生的（伪）随机数，来模拟对量子态观测所导致的（真）随机结果。正确执行实验流程，最终的结果应该为\"0\"与\"1\"的频率各占50%左右。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister, execute, Aer\n",
    "\n",
    "# 如果这里运行出错，请检查是否已经正确安装qiskit包\n",
    "# qiskit安装指令：pip install qiskit\n",
    "# qiskit安装教程：https://blog.csdn.net/dong01xxx/article/details/96574810\n",
    "# qiskit官方网站：https://www.qiskit.org/\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 初始化\n",
    "simulator = Aer.get_backend('qasm_simulator') # 获取模拟器\n",
    "qreg_q = QuantumRegister(1, 'q')   # 创建一个1位量子寄存器\n",
    "creg_c = ClassicalRegister(1, 'c') # 创建一个1位经典寄存器\n",
    "\n",
    "# 电路搭建\n",
    "circuit = QuantumCircuit(qreg_q, creg_c) # 电路创建\n",
    "circuit.h(qreg_q[0])                     # H门\n",
    "circuit.measure(qreg_q[0], creg_c[0])    # 测量门\n",
    "# 绘制电路\n",
    "circuit.draw()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "创建量子电路时，所有的量子寄存器会默认置零，对于本例而言，当前量子态为：\n",
    "\n",
    "$\\begin{equation}\n",
    "|\\psi_0\\rangle=|0\\rangle\n",
    "\\end{equation}$\n",
    "\n",
    "对其施加H门后，由$|0\\rangle$态转化为$|+\\rangle$态：\n",
    "$\\begin{equation}\n",
    "|\\psi_1\\rangle=H|\\psi_0\\rangle\n",
    "=|+\\rangle\n",
    "\\end{equation}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 电路运行和数据处理\n",
    "total_shots = 1024 # 对于小型实验，一般重复进行1024次\n",
    "job = execute(circuit, simulator, shots = total_shots) # 执行电路并将结果存入job\n",
    "result_dict = job.result().get_counts(circuit) # 从job中读取0/1字典\n",
    "\n",
    "# 输出结果\n",
    "print(result_dict) # 直接输出该字典"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "对观测结果的期望进行计算：\n",
    "\n",
    "$\\begin{equation}\n",
    "P_0=|\\langle0|\\psi_1\\rangle|^2=\n",
    "|\\frac{1}{\\sqrt{2}}|^2=50\\%\n",
    "\\end{equation}$\n",
    "\n",
    "$\\begin{equation}\n",
    "P_1=|\\langle1|\\psi_1\\rangle|^2=\n",
    "|\\frac{1}{\\sqrt{2}}|^2=50\\%\n",
    "\\end{equation}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from matplotlib import pyplot as plt\n",
    "# 对观测结果进行统计分析\n",
    "result = list(result_dict.items())\n",
    "result.sort(key = lambda x:x[1], reverse = True) # 将结果按频数从大到小排列\n",
    "result_type = [i[0] for i in result]\n",
    "result_frequency = [i[1] / total_shots for i in result]\n",
    "plt.bar(result_type, result_frequency, align = 'center')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "本次实验中，我们将大量的单量子比特初始化后通过一个H门，然后观测其状态，得到结果：有约50%的单量子比特显现出$|0\\rangle$态、另外约50%的单量子比特显现出$|1\\rangle$态。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
